<?php
namespace juhe;
use curl\MyCurl;

class Api
{
    protected $apiKey=[];
    protected $error;

    public function __construct($config=[])
    {
        $this->apiKey = $config;
    }

    public function getError(){
        return $this->error;
    }

    public function setApiKey($key,$value){
        $this->apiKey[$key] = $value;
    }

    #银行卡类型真伪
    public function bankCardApi($bankcard){
        $url = sprintf("http://detectionBankCard.api.juhe.cn/bankCard?key=%s&cardid=%s",$this->apiKey["juhe_key_bankcard_number"],$bankcard);
        $mycurl = new MyCurl();
        $ret = $mycurl->send($url);
        $ret = json_decode($ret,true);
        if($ret["error_code"]==0){
            return $ret['result'];
        }else{
            $this->error = $ret["reason"];
            return false;
        }
    }

    #银行卡四要素认证
    public function bankApi($bankcard,$realname,$idcard,$mobile){
        $url = sprintf("http://v.juhe.cn/verifybankcard4/query?key=%s&bankcard=%s&realname=%s&idcard=%s&mobile=%s",$this->apiKey["juhe_key_bankcard"],$bankcard,$realname,$idcard,$mobile);
        $mycurl = new MyCurl();
        $ret = $mycurl->send($url);
        $ret = json_decode($ret,true);
        if($ret["error_code"]==0){
            return $ret['result'];
        }else{
            $this->error = $ret["reason"];
            return false;
        }
    }

    #身份证识别
    public function idimageApi($image_file,$side){
        $url = "http://apis.juhe.cn/idimage/verify";
        $postData = [
            "key"=>$this->apiKey["juhe_key_idcard"],
            "image"=>base64_encode(file_get_contents($image_file)),
            "side"=>$side,
        ];
        $mycurl = new MyCurl();
        $ret = $mycurl->send($url,$postData,"post");
        $ret = json_decode($ret,true);
        if($ret["error_code"]==0){
            return $ret['result'];
        }else{
            $this->error = $ret["reason"];
            return false;
        }
    }

    #实名认证
    public function idcardApi($id_card_number,$real_name){
        $url = "http://op.juhe.cn/idcard/query?key=".$this->apiKey['juhe_key_realname']."&idcard=%s&realname=%s";
        $url = sprintf($url,$id_card_number,urlencode($real_name));
        $mycurl = new MyCurl();
        $ret = $mycurl->send($url);
        $ret = json_decode($ret,true);
        if($ret["error_code"]==0){
            return $ret['result'];
        }else{
            $this->error = $ret["reason"];
            return false;
        }
    }

    #三网手机实名制认证
    public function telecomApi($id_card_number,$real_name,$mobile){
        $url = "http://v.juhe.cn/telecom/query?key=".$this->apiKey['juhe_key_telecom']."&idcard=%s&realname=%s&mobile=%s";
        $url = sprintf($url,$id_card_number,urlencode($real_name),$mobile);
        $mycurl = new MyCurl();
        $ret = $mycurl->send($url);
        $ret = json_decode($ret,true);
        if($ret["error_code"]==0){
            if($ret["result"]["res"]==1){
                return $ret["result"];
            }else{
                $this->error = "三网手机实名制认证未通过";
                return false;
            }
        }else{
            $this->error = $ret["reason"];
            return false;
        }
    }

}